package controller

import (
	"context"
	"github.com/RaymondCode/simple-demo/config"
	"github.com/RaymondCode/simple-demo/entity/myerr"
	"github.com/RaymondCode/simple-demo/entity/param"
	"github.com/RaymondCode/simple-demo/entity/response"
	"github.com/RaymondCode/simple-demo/middleware"
	serviceimpl "github.com/RaymondCode/simple-demo/service/impl"
	"github.com/RaymondCode/simple-demo/util/webutil"
	"github.com/cloudwego/hertz/pkg/app"
	"log"
	"net/http"
	"strconv"
)

var favoriteService = serviceimpl.NewFavoriteServiceInstance()

// Like 点赞、取消赞操作
func Like(ctx context.Context, c *app.RequestContext) {
	var favoriteParam param.Favorite
	var err error
	favoriteParam.ActionType, err = strconv.Atoi(c.Query("action_type"))
	favoriteParam.VideoID, err = strconv.Atoi(c.Query("video_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ArgumentError(myerr.ArgumentInvalid(webutil.GetValidMsg(err, favoriteParam))))
		return
	}
	userId, err := strconv.Atoi(middleware.ThreadLocal.Get().(map[string]string)[config.Config.ThreadLocal.Keys.UserId])
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	err = favoriteService.Like(&favoriteParam, userId)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.Ok)
}

// FavoriteList 查看点赞列表
func FavoriteList(ctx context.Context, c *app.RequestContext) {
	var favoriteListParam param.FavoriteList
	var err error
	favoriteListParam.UserId, err = strconv.Atoi(c.Query("user_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ArgumentError(myerr.ArgumentInvalid(webutil.GetValidMsg(err, favoriteListParam))))
		return
	}
	videoList, err := favoriteService.FavoriteList(favoriteListParam.UserId)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.FavoriteList{
		Response: response.Ok,
		Data:     videoList,
	})
}
